<template>
  <page-header-wrapper>
    <a-card :bordered="false">
      <div class="table-page-search-wrapper">
        <a-form layout="inline">
          <a-row :gutter="24">
            <a-col :md="4" :sm="24">
              <a-form-item>
                <a-input placeholder="请输入活动名称" v-model="queryParam.name" />
              </a-form-item>
            </a-col>
            <a-col :md="4" :sm="24">
              <span class="table-page-search-submitButtons">
                <a-button type="primary" @click="handleSearch(true)" icon="search">查询</a-button>
              </span>
            </a-col>
            <a-col :md="1" :sm="24" :offset="14">
              <a-button type="primary" icon="plus" @click="handleAdd">新增</a-button>
            </a-col>
          </a-row>
        </a-form>
      </div>
      <s-table
        ref="table"
        size="default"
        rowKey="id"
        :columns="columns"
        :data="loadData"
        showPagination="auto"
        :pagination="pagination"
      >
        <template slot="status" slot-scope="status">
          <a-tag :color="kindsStatusDisplay(status)">{{ status | kindsStatusFilter }}</a-tag>
        </template>
        <a-badge :status="runTimeStatus(text)" :text="runTimeDisplay(text)" slot="runtime" slot-scope="text" />
        <span slot="action" slot-scope="text, record">
          <template>
            <a @click="handleEdit(record)">编辑</a>
            <a-divider type="vertical" />
            <a @click="handleStatus(record)">{{ record.status === 1 ? '下架' : '上架' }}</a>
            <a-divider type="vertical" />
            <a @click="handleCity(record)">查看城市</a>
          </template>
        </span>
      </s-table>
      <create-form ref="createModal" @ok="handleSearch" :addAction="addAction" :editAction="editAction" />
    </a-card>
  </page-header-wrapper>
</template>

<script>
import { clothesStoreActivityLists, clothesStoreActivityAdd, clothesStoreActivityEdit, clothesStoreActivityPutaway, clothesStoreActivityDownfall } from '@/api/activity'
import orderStatus from '@/mixins/orderStatus'
import formTableMixin from '@/mixins/formTableMixin'
import runtimeStatus from '@/mixins/runtimeStatus'
import CreateForm from './modules/ClothesStoreActivityForm'

const columns = [
  {
    title: '活动名称',
    dataIndex: 'name'
  },
  {
    title: '价格',
    dataIndex: 'price',
    customRender (text) {
      return '￥' + text
    }
  },
  {
    title: '数量',
    dataIndex: 'number',
  },
  {
    title: '内容',
    dataIndex: 'content',
  },
  {
    title: '开始时间',
    dataIndex: 'atime',
  },
  {
    title: '结束时间',
    dataIndex: 'etime',
    width: 120
  },
	{
    title: '销量',
    dataIndex: 'sales'
  },
  {
    title: '权重',
    dataIndex: 'weight',
  },
  {
    title: '状态',
    dataIndex: 'runtime',
    scopedSlots: { customRender: 'runtime' }
  },
  {
    title: '是否上架',
    dataIndex: 'status',
    scopedSlots: { customRender: 'status' },
  },
  {
    title: '操作',
    dataIndex: 'action',
    width: '200px',
    scopedSlots: { customRender: 'action' }
  }
]

export default {
  name: 'ClothesStoreActivityList',
  mixins: [orderStatus, formTableMixin, runtimeStatus],
  components: {
    CreateForm
  },
  data () {
    this.columns = columns
    this.editAction = clothesStoreActivityEdit
    this.addAction = clothesStoreActivityAdd
    this.listAction = parameter =>
      clothesStoreActivityLists(parameter).then(res => {
        const data = res.data.data
        data.forEach(item => {
          const status = this.getRunTime(item.atime, item.etime)
          item.runtime = status
        })
        return res
      })
    return {}
  },
  methods: {
    handleStatus (record) {
      this.$confirm({
        title: '你确定要进行此项操作吗?',
        content: (record.status === 1 ? '下架' : '上架') + '活动',
        onOk: () => {
          const action = record.status === 1 ? clothesStoreActivityDownfall : clothesStoreActivityPutaway
          action({ id: record.id }).then(res => {
            this.$refs.table.refresh()
            this.$message.success('操作成功')
          }).catch(() => { })
        }
      })
    },
    handleCity (record) {
      this.$router.push({
        name: 'activityCity',
        params: {
          activity_id: record.id,
        }
      })
    }
  }
}
</script>
